﻿using DAO;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Sklad.WindowsApplication
{
    public partial class MainEmployeeForm : Form
    {
        private DAO.DaoRepository dao;
        public delegate void BindGrid();
        public static BindGrid bg;
        public delegate void BindLB();
        public static BindLB bindListBox;
        public MainEmployeeForm()
        {
            bg = this.BindDataGrid;
            bindListBox = this.BindListBox;
            dao = new DAO.DaoRepository();
            InitializeComponent();
            this.BindDataGrid();
            this.BindListBox();         
        }

        private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (listBox1.SelectedValue == null)
            {
                return;
            }
            int id_cat = Int32.Parse(listBox1.SelectedValue.ToString());
            BindingSource bs = new BindingSource();
            bs.DataSource = this.DataGridSource().Where(t => t.Id_Category == id_cat);
            dataGridView1.DataSource = bs;
        }

        private void listBox1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                if (listBox1.SelectedValue == null)
                {
                    return;
                }
                DialogResult res = MessageBox.Show("При удалении категории будут удалены все связанные с ней товары. Продолжить?", "Удаление категории", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                if (res == System.Windows.Forms.DialogResult.No)
                    return;
                int id_cat = Int32.Parse(listBox1.SelectedValue.ToString());
                dao.CategoryRepository.Delete(id_cat);
                this.BindListBox();
                this.BindDataGrid();
            }
        }

        private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            string s = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            int id_prod = Int32.Parse(s);
            dataGridView1.CurrentRow.Selected = true;
            EditProduct edit = new EditProduct(id_prod);
            edit.Visible = true;
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.BindDataGrid();
        }

        private void BindListBox()
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = dao.CategoryRepository.GetAll().ToList<Category>();
            listBox1.DataSource = bs;
            listBox1.ValueMember = "Id";
            listBox1.DisplayMember = "Name";
        }

        private IEnumerable<GridProduct> DataGridSource()
        {
            var products = from p in dao.ProductRepository.GetAll().ToList<Product>()
                           join c in dao.CategoryRepository.GetAll().ToList<Category>() on p.Id_Category equals c.Id
                           select new GridProduct
                           {
                               Id = p.Id,
                               Name = p.Name,
                               CategoryName = c.Name,
                               Count = p.Count,
                               Description = p.Description,
                               Id_Category = c.Id
                           };
            return products.AsEnumerable<GridProduct>();
        }

        public void BindDataGrid()
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = this.DataGridSource();
            dataGridView1.DataSource = bs;
            dataGridView1.Columns["Id_Category"].Visible = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.BindDataGrid();
        }

        private void добавитьToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            AddProduct add = new AddProduct();
            add.Visible = true;
        }

        //private void bt_send_rec_Click(object sender, EventArgs e)
        //{
        //    SendRecieveForm srf = new SendRecieveForm();
        //    srf.Visible = true;
        //}

        private void добавитьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddCategory add = new AddCategory();
            add.Visible = true;
        }

 

        private void принятьТоварToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddReceiveForm add = new AddReceiveForm();
            add.Visible = true;
        }

        private void отгрузитьТоварToolStripMenuItem_Click(object sender, EventArgs e)
        {//для отдачи. send - отдать, receive принять
            SendForm send = new SendForm();
            send.Visible = true;
        }

        private void добавитьПоставщикаToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddProviderForm add = new AddProviderForm();
            add.Visible = true;
        }

        private void типыКонтрагентовToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            ContragentTypes ct = new ContragentTypes();
            ct.Visible = true;
        }
    }
}
